package com.gym.openapi.web.controller;

import com.gym.openapi.common.Const;
import com.gym.openapi.common.ResponseResult;
import com.gym.openapi.common.ResultTable;
import com.gym.openapi.entity.AdminUser;
import com.gym.openapi.entity.Menu;
import com.gym.openapi.services.MenuServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author ming
 * 2021/11/24
 * description:
 * * @RestController//类中所有方法返回json
 * * @RequestMapping来映射请求，也就是通过它来指定控制器可以处理哪些URL请求，相当于Servlet中在web.xml中配置
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuServices menuServices;


    @RequestMapping("/tree")
    public List<Menu> getTree() {
        return menuServices.getAllTree();
    }




    /**
     * @author:ming
     * @Date: 2021/11/24
     * @description:左边菜单导航栏设置 返回统一类型用ResponseResult
     */
    @RequestMapping("/getMenu")
    public ResponseResult getMenu(HttpSession session) {
        //session.getAttribute("")取到的类型是是object
        //所以赋值前要强转一下(AdminUser)
        AdminUser user = (AdminUser) session.getAttribute(Const.IS_LOGIN);
        if (user == null) {
            return ResponseResult.getFail("先上线");
        }
        //访问数据库,获取用户有权访问的菜单
        List<Menu> menus = menuServices.getMenuByUid(user.getId());
        return ResponseResult.getSuccess(menus);
    }



    /**
     * @author:ming
     * @Date: 2021/11/25
     * @description:菜单管理显示页面
     */
    @RequestMapping("/list")
    public ResultTable getLst(Integer page, Integer limit) {
        return menuServices.getList(page, limit);
    }

    /**
     * @author:ming
     * @Date: 2021/11/25
     * @description:菜单根据id显示可操作的权限内容
     */
    @RequestMapping("/info")
    public Menu getLst(Integer id) {
        return menuServices.getMenuById(id);
    }

    /**
     * @author:ming
     * @Date: 2021/11/27
     * @description:编辑修改信息
     */
    @RequestMapping("/update")
    public ResponseResult update(Menu menu) {
        return menuServices.update(menu);
    }

    /**
     * @author:ming
     * @Date: 2021/11/27
     * @description:添加数据
     */
    @RequestMapping("/add")
    public ResponseResult add(Menu menu) {
        return menuServices.add(menu);
    }
}
